<script setup>
const {proxy} = getCurrentInstance()
const applyUserList = ref([])
const applyUserListOpen = ref(false)
const applyUserListTotal = ref(0)
const applyUserListTitle = ref('')
const applyUserListLoading = ref(false)
const emits = defineEmits(['handleSelectedUser', 'getApplyUserList'])
const data = reactive({
  applyUserListQueryParams: {
    pageNum: 1,
    pageSize: 10,
    nickName: '',
    phoneNumber: '',
    deptName: ''
  }
})
const {applyUserListQueryParams} = toRefs(data)


function openDialog() {
  applyUserListOpen.value = true
  applyUserListTitle.value = "选择用户"
  getApplyUserList()
}

function getApplyUserList() {
  emits('getApplyUserList', applyUserListQueryParams.value, (res) => {
    applyUserListLoading.value = false
    applyUserList.value = res.list
    applyUserListTotal.value = res.total
  })
}

function confirmSelectedUser(user) {
  applyUserListOpen.value = false
  emits('handleSelectedUser', user)
}

/** 搜索按钮操作 */
function handleQueryForApplyUserList() {
  getApplyUserList()
}

/** 重置按钮操作 */
function resetQueryForApplyUserList() {
  proxy.resetForm("applyUserListQueryRef")
  handleQueryForApplyUserList()
}

defineExpose({
  openDialog
})

</script>

<template>
  <!-- 选择用户对话框 -->
  <el-dialog :title="applyUserListTitle" v-model="applyUserListOpen" width="1000px" append-to-body :close-on-click-modal="false">
    <el-row>
      <el-form :model="applyUserListQueryParams" ref="applyUserListQueryRef">
        <el-row :gutter="30">
          <el-col :span="6" :xs="24">
            <el-form-item label="真实姓名" prop="nickName">
              <el-input
                  v-model="applyUserListQueryParams.nickName"
                  placeholder="请输入真实姓名"
                  clearable
                  style="width: 240px"
                  @keyup.enter="handleQueryForApplyUserList"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6" :xs="24">
            <el-form-item label="手机号码" prop="phoneNumber">
              <el-input
                  v-model="applyUserListQueryParams.phoneNumber"
                  placeholder="请输入手机号码"
                  clearable
                  style="width: 240px"
                  @keyup.enter="handleQueryForApplyUserList"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6" :xs="24">
            <el-form-item label="部门名称" prop="deptName">
              <el-input
                  v-model="applyUserListQueryParams.deptName"
                  placeholder="请输入部门名称"
                  clearable
                  style="width: 240px"
                  @keyup.enter="handleQueryForApplyUserList"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6" :xs="24">
            <el-form-item >
              <el-button type="primary" icon="Search" @click="handleQueryForApplyUserList">搜索</el-button>
              <el-button icon="Refresh" @click="resetQueryForApplyUserList">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
    </el-row>
    <el-row>
      <el-table v-loading="applyUserListLoading" :data="applyUserList">
        <el-table-column label="真实姓名" key="nickName" prop="nickName" width="130"/>
        <el-table-column label="性别" key="sex" prop="sex" width="60"/>
        <el-table-column label="手机号" key="phoneNumber" prop="phoneNumber" width="150"/>
        <el-table-column label="部门名称" key="deptName" prop="deptName" :show-overflow-tooltip="true" />
        <el-table-column label="操作" align="left" fixed="right" width="80" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button type="success" plain @click="confirmSelectedUser(scope.row)" >确定</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
          v-show="applyUserListTotal > 0"
          :total="applyUserListTotal"
          v-model:page="applyUserListQueryParams.pageNum"
          v-model:limit="applyUserListQueryParams.pageSize"
          @pagination="getApplyUserList"
      />
    </el-row>
  </el-dialog>
</template>

<style scoped lang="scss">

</style>